import 'package:flustars/flustars.dart';

class kyDefaultsUtils {
  static Future<bool?> saveString(String key, String value) async{
    return SpUtil.putString(key, value);
  }

  static String? getStringWithKey(String key) {
    return SpUtil.getString(key);
  }

  static Future<bool>? saveBool(String key, bool value) {
    return SpUtil.putBool(key, value);
  }

  static bool? getBoolWithKey(String key) {
    return SpUtil.getBool(key);
  }

  static Future<bool>? saveModel(String key, Object model) {
    return SpUtil.putObject(key, model);
  }

  static getModelWithKey(String key) {
    Map? json = SpUtil.getObject(key);
    return json;
  }

  static Future<bool>? saveModelsArr(String key, List<Object> modelsArr) {
    return SpUtil.putObjectList(key, modelsArr);
  }

  static getModelsArrWithKey(String key) {
    List<Map<dynamic, dynamic>> json = SpUtil.getObjectList(key)!;
    return json;
  }

  static Future<bool>? removeWithKey(String key) {
    return SpUtil.remove(key);
  }
}


String retrunValueWithKey(String key,Map <String, dynamic>json){
  if (json.containsKey(key)) {

    // LogUtils.d('json[key]==$key====${json[key]}');
    if (json[key] is String) {
      return json[key];
    }else if (json[key] is num) {
      return json[key].toString();
    }else if (json[key] is bool) {
      return json[key].toString();
    }
  }
  return '';
}

/**
 * target  要转换的数字
 * postion 要保留的位数
 * isCrop  true 直接裁剪 false 四舍五入
 */
String formartNum(num target, int postion, {bool isCrop = false}) {
  String t = target.toString();
  // 如果要保留的长度小于等于0 直接返回当前字符串
  if (postion < 0) {
    return t;
  }
  if (t.contains(".")) {
    String t1 = t.split(".").last;
    if (t1.length >= postion) {
      if (isCrop) {
        // 直接裁剪
        return t.substring(0, t.length - (t1.length - postion));
      } else {
        // 四舍五入
        return target.toStringAsFixed(postion);
      }
    } else {
      // 不够位数的补相应个数的0
      String t2 = "";
      for (int i = 0; i < postion - t1.length; i++) {
        t2 += "0";
      }
      return t + t2;
    }
  } else {
    // 不含小数的部分补点和相应的0
    String t3 =  postion>0?".":"";

    for (int i = 0; i < postion; i++) {
      t3 += "0";
    }
    return t + t3;
  }
}
